package com.iscas.tech.service.impl;

import com.iscas.tech.entity.AtNewsattach;
import com.iscas.tech.entity.AtNewsinfo;
import com.iscas.tech.mapper.AtNewsinfoMapper;
import com.iscas.tech.param.PageParam;
import com.iscas.tech.service.AtNewsattachService;
import com.iscas.tech.service.AtNewsinfoService;
import com.iscas.tech.utils.DataUtil;
import com.iscas.tech.vo.AtNewsinfoQueryVo;
import com.iscas.tech.vo.FileWithNews;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 新闻信息表 服务实现类
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@Service
public class AtNewsinfoServiceImpl extends BaseServiceImpl<AtNewsinfoMapper, AtNewsinfo> implements AtNewsinfoService {

    @Autowired
    private AtNewsinfoMapper atNewsinfoMapper;
    @Autowired
    private AtNewsattachService mNewsService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveNews(FileWithNews param) throws Exception {
        if (param.getIsPublish() == null) {
            param.setIsPublish(0);
        }
        param.setnBwnum(0);
        param.setIsAttach(0);
        if (atNewsinfoMapper.saveNews(param)) {
            if (param.getFileList() != null) {
                if (param.getFileList().size() > 0) {
                    param.setIsAttach(1);
                }
                for (AtNewsattach newsInfo : DataUtil.getNewsFileList(param.getId(), param.getnType(), param.getCreateTitle(), param.getFileList())) {
                    if (!mNewsService.saveAtNewsattach(newsInfo)) {
                        throw new Exception();
                    }
                }
            }
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateNewsInfo(FileWithNews param) throws Exception {
        param.setIsAttach(0);
        if (param.getFileList() != null && param.getFileList().size() > 0) {
            param.setIsAttach(1);
        }
        //  修改基础信息
        if (super.updateById(param.transform())) {
            //  删除原有附件信息
            mNewsService.deleteByNewsId(param.getId());
            //  新增附件信息
            if (param.getFileList() != null) {
                for (AtNewsattach news : DataUtil.getNewsFileList(param.getId(), param.getnType(), param.getCreateTitle(), param.getFileList())) {
                    if (!mNewsService.saveAtNewsattach(news)) {
                        throw new Exception();
                    }
                }
            }
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteNewsInfo(String ids) throws Exception {
        if (ids == null) {
            return true;
        }
        String[] idList = ids.split(",");
        for (String id : idList) {
            if (!atNewsinfoMapper.deleteNewsInfo(Integer.parseInt(id))) {
                throw new Exception();
            }
        }
        return true;
    }

    @Override
    public Paging<AtNewsinfoQueryVo> getNewsPageList(PageParam pageParam, Integer newsType) throws Exception {
        List<AtNewsinfoQueryVo> pageList = atNewsinfoMapper.getNewsPageList(pageParam.getOffset(), pageParam.getPageSize(), newsType);
        Paging paging = new Paging();
        paging.setTotal(atNewsinfoMapper.getNewsCount(newsType));
        paging.setRecords(pageList);
        return paging;
    }

    @Override
    public FileWithNews get(Integer id) throws Exception {
        return atNewsinfoMapper.get(id);
    }

    @Override
    public Boolean releaseNews(Integer id) {
        return atNewsinfoMapper.releaseNews(id);
    }

}
